<?php
session_start();
include_once("config/config.inc.php");
include_once("dailyreport.function.php");
$act = $_POST["act"];
$user_id = $_SESSION["hotel"]["userid"];
$amount = $_POST["new_tendered1"];

$datetime = date("Y-m-d H:i:s");
$shift = "end";
$amount = $_POST["new_tendered1"];
$deno_1 = $_POST["txt1"];
$deno_5 = $_POST["txt5"];
$deno_10 = $_POST["txt10"];
$deno_20 = $_POST["txt20"];
$deno_50 = $_POST["txt50"];
$deno_100 = $_POST["txt100"];
$deno_500 = $_POST["txt500"];
$deno_1000 = $_POST["txt1000"];
$oic_user_id = "";
$to_user_id = "";
$mid = $_GET["mid"];


//lobbyid
$lsql = "select settings_value from settings where id = '3'";
$lres = mysql_query($lsql);
list($lobbyid)=mysql_fetch_row($lres);


$sql = "select shift,`datetime` from `shift-transactions` order by datetime desc limit 0,1";
$res = mysql_query($sql) or die($sql);
list($shift,$startdate)=mysql_fetch_row($res);
if($shift=='start'&&!$mid)
{
	
	list($d, $t) = explode(" ", $datetime);
	list($h, $m, $s) = explode(":", $t);	
	$shift = getshift($startdate);	
	list($dd, $tt) = explode(" ", $startdate);	
	$dt = strtotime($datetime);
	//echo "$startdate $shift , $datetime > $dd 23:45:00";
	if(!(($shift=="1st"&&$dt>=strtotime("$dd 07:45:00"))||($shift=="2nd"&&$dt>=strtotime("$dd 15:45:00"))||($shift=="3rd"&&$dt>=strtotime("$dd 23:45:00"))))
	{
		if($shift=="1st")$time="7:45 AM";
		if($shift=="2nd")$time="3:45 PM";
		if($shift=="3rd")$time="11:45 PM";
		
		die("You cannot access this page yet. You can access this page on $time.");
	}	
}
function getshift($date) {
	if(!$date)$date=date("Y-m-d H:i:s");
	list($d, $t) = explode(" ", $date);
	list($h, $m, $s) = explode(":", $t);	
	$sql = "select shift_id from shifts where $h between shift_start and shift_end";
	$res = mysql_query($sql) or die(mysql_error() .$sql);
	list($shift)=mysql_fetch_row($res);
	
	if($h>=16&&$h<=23)
	{
		return $shift = "3rd";
	}
	elseif($h>=8&&$h<=15)
	{
		return $shift = "2nd";
	}
	return  "1st";
}

if($act == "Save")
{

	if(!$mid)
	{
		$sql = "insert into cash_on_hand(user_id,amount,deno_1,deno_5,deno_10,deno_20,deno_50,deno_100,deno_500,deno_1000,datetime) 
			values('$user_id','$amount','$deno_1','$deno_5','$deno_10','$deno_20','$deno_50','$deno_100','$deno_500','$deno_1000','$datetime')";
		mysql_query($sql);
		$newid = mysql_insert_id();
		$sql = "insert into `shift-transactions`(shift,user_id,oic_user_id,to_user_id,cash_on_hand_id,datetime) 
			values('end','$user_id','$oic_user_id','$to_user_id','$newid','$datetime')";
		mysql_query($sql) or die($sql . mysql_error());
		$sql = "insert into safekeep(safekeep_date,cashier_id,oic_id,amount,current_amount)
			values('$datetime','$user_id','','','0')";
		mysql_query($sql);
			//0 current_cash
		$sql = "insert into current_cash(cc_date,transaction_amount,current_amount,transaction_type,update_by) values('$datetime','0','0','Out','$user_id')";
		mysql_query($sql);
		$now = date("Y-m-d H:i:s");
		$msg = "Endshift report printed: $now | COH: $newid\n";
		$fp = fopen('reports/endshift.log','a');
		fwrite($fp, $msg);
		fclose($fp);
	
		$l = 1;
	}else{
		$l=0;
		$now = date("Y-m-d H:i:s");
		$msg = "MID-shift report printed: $now | COH: $newid\n";
		$fp = fopen('reports/endshift.log','a');
		fwrite($fp, $msg);
		fclose($fp);
	}
	//$retval=getPrintableCDailyReport($amount);
	$retval=chr(hexdec('1B')).chr(hexdec('4D')).chr(1);	
	$retval.= getReport($amount,$l,$lobbyid)."\n\n\n\n\n";	
	$retval.=chr(hexdec('1D')).chr(hexdec('56')).chr(49);
	$stamp = date("YmdHis");
	$file = "cdailyreport$stamp.txt";
	$fp = fopen("reports/" .$file, "w");
	fwrite( $fp,$retval);
	fclose($fp);
	shell_exec('c:\pstools\psexec c:\xampp\htdocs\fds\ajax\p.bat ' . $file);
	$dbfile = 'shogun2db_'.$stamp .'.sql';
	shell_exec('c:\pstools\psexec dbbackup.bat ' . $dbfile );
	
	session_start();
	unset($_SESSION);
	session_destroy();
	echo "<script language='javascript'>parent.document.location.href='../index.php'</script>";
	
	/**if(!hasShiftStart())
	{	
		echo "<script language='javascript'>parent.document.location.href='shiftstart.php'</script>";	
	}else{
		
	}*/
}

function hasShiftStart()
{
	$sql = "select shift from `shift-transactions` order by datetime desc limit 0,1";
	$res = mysql_query($sql) or die($sql);
	list($shift)=mysql_fetch_row($res);
	
	if($shift=='end')
	{
		return 0;
	}
	return 1;
}

function checkShiftEndIsAvailable()
{
	/*//if shift-transaction is still in start mode
	$sql = "select shift from `shift-transactions` order by datetime desc ";
	$res = mysql_query($sql);
	$shift = '';
	if($row = mysql_fetch_array($res))
	{
		$shift = $row["shift"];
	}
	if($shift == 'end')
	{
		return false;	
	}

	//if expected shift end time is before 15 mins
	$now = date("Y-m-d H:i:s");
	$sql = "select datetime from `shift-transactions` order by datetime desc";
	$res = mysql_query($sql);
	$dt="";
	if($row = mysql_fetch_array($res))
	{
		$dt = $row["datetime"];
	}
	list($date,$time)=explode(" ",$now);
	list($hour,$min,$sec)=explode(":",$time);
	if(getShiftEndTimeByDatetime($dt) == "23:45:00")
	{
		if("07:45:00" > $time)
		{
			return true;
		}
	}
	if(getShiftEndTimeByDatetime($dt)>=$time)
	{
		return true;
	}*/
	return true;
	
}

function getShiftEndTimeByDatetime($datetime)
{

	list($date,$time)=explode(" ",$datetime);
	list($hour,$min,$sec)=explode(":",$time);

	
	$shifttime1 = "07:45:00";
	$shifttime2 = "15:45:00";
	$shifttime3 = "23:45:00";
	if($time >= $shifttime1 && $time <= $shifttime2)
	{
		return "15:45:00";	
	}
	else if($time >= $shifttime2 && $time <= $shifttime3)
	{
		return "23:45:00";
	}
	else if($time >= $shifttime3)
	{
		return "07:45:00";
	}
}

if(checkShiftEndIsAvailable())
{
?>
<style>
.cmdbtn {
	width:100px;
	border:1px solid  #ffffff;
	background-color: #9BD1E6;
	margin-right:2px;
	padding:2px;
	cursor:pointer;
}

.keypadfield{ border:1px solid #51B1D8; background-color:#E0EBEF; width:100%}
</style>
<form name='myform'  id="myform" method='post'>
<div><b>
<?
if($mid)
{?>MID SHIFT<?
}else{?>SHIFT END<?}?>
</b></div>
<table>
<tr>
	<td style="vertical-align:top">Cash on Hand:</td>
	<td>
		<table cellpadding=0 cellspacing=3  />
		<tr><td style="text-align:right">1</td>
			<td width="70"><input type="text" id="txt1" name="txt1"  size="5" class="keypadfield" style="text-align:right" /></td>
			<td><input type="text" id="txttotal1" name="txttxttotal1"  size="7" style="text-align:right;background-color: white" disabled /></td></tr>
		<tr><td style="text-align:right">5</td>
			<td><input type="text" id="txt5" name="txt5" size="5" class="keypadfield" style="text-align:right"/></td>
			<td><input type="text" id="txttotal5" name="txttxttotal5"  size="7" style="text-align:right;background-color: white" disabled /></td></tr>
		<tr><td style="text-align:right">10</td>
			<td><input type="text" id="txt10" name="txt10" size="5" class="keypadfield" style="text-align:right"/></td>
			<td><input type="text" id="txttotal10" name="txttxttotal10"  size="7" style="text-align:right;background-color: white" disabled /></td></tr>
		<tr><td style="text-align:right">20</td>
			<td><input type="text" id="txt20" name="txt20" size="5" class="keypadfield" style="text-align:right"/></td>
			<td><input type="text" id="txttotal20" name="txttxttotal20"  size="7" style="text-align:right;background-color: white" disabled /></td></tr>
		<tr><td style="text-align:right">50</td>
			<td><input type="text" id="txt50" name="txt50" size="5" class="keypadfield" style="text-align:right"/></td>
			<td><input type="text" id="txttotal50" name="txttxttotal50"  size="7" style="text-align:right;background-color: white" disabled /></td></tr>
		<tr><td style="text-align:right">100</td>
			<td><input type="text" id="txt100" name="txt100" size="5" class="keypadfield" style="text-align:right"/></td>
			<td><input type="text" id="txttotal100" name="txttxttotal100"  size="7" style="text-align:right;background-color: white" disabled /></td></tr>
		<tr><td style="text-align:right">500</td>
			<td><input type="text" id="txt500" name="txt500" size="5" class="keypadfield" style="text-align:right"/></td>
			<td><input type="text" id="txttotal500" name="txttotal500" size="7" style="text-align:right;background-color: white" disabled /></td></tr>
		<tr><td style="text-align:right">1000</td>
			<td><input type="text" id="txt1000" name="txt1000" size="5" class="keypadfield" style="text-align:right"/></td>
			<td><input type="text" id="txttotal1000" name="txttotal1000" size="7" style="text-align:right;background-color: white" disabled /></td></tr>
		<tr><td style="text-align:right"><b>Total:</b></td>
			<td colspan="2"><input type="text"  class="money" id="new_tendered" name="new_tendered" value="0" size="16" style="text-align:right;background-color: white;font-weight:bold;" disabled />
			<input type="hidden" name="new_tendered1" id="new_tendered1" />
			</tr>			
		<tr><td><input type="button" class="denomination" value="Clear" /></td></tr>
		</table>
	</td>
</tr>
<tr>
<td>&nbsp;</td><td>&nbsp;</td>
</tr>
<tr>
	<td>&nbsp;</td>
	<td><input name="act" id="act" type="submit" value="Save" class="cmdbtn" /></td>
</tr>
</table>
</form>
<style>
table{font-family:lucida,arial,helvetica;font-size:.9em}
</style>
<link rel="stylesheet" type="text/css" href="../css/jquery.keypad.css" />
<link rel="stylesheet" type="text/css" href="../css/start/jquery-ui.css" />
<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript" src="../js/jquery.keypad.pack.js"></script>
<script type="text/javascript" src="../js/jquery-ui.js"></script>
<script lang="javascript">

$(document).ready(function(){
	$(".keypadfield").keypad({keypadOnly:true});
	//$(".keypadfield").onchange(function() {
	//	var _mulnewval = $($(this).val()).val()*1;
	//	 $($(this).name()).val( _mulnewval); //multiplier
	//});
	  	
	

	$(".keypadfield").change(function(event) {
		var _mulnewval = $(this).val()*1;	
		var _deno = $(this).attr('name').match(/\d+/)[0]*1;
		var _newtotval =  _mulnewval * _deno;
		$("#txttotal"+_deno).val(_newtotval)	

		var curval = $("#new_tendered").val() * 1;
		var arr = [ "txttotal1", "txttotal5", "txttotal10", "txttotal20", "txttotal50", "txttotal100", "txttotal500" ,  "txttotal1000"];
		var _ftotal = 0;
		
		jQuery.each(arr, function() {
			var _val = $("#" + this).val() * 1;
			_ftotal =  _val + _ftotal; 
		});
		$("#new_tendered").val(_ftotal);		
		$("#new_tendered1").val(_ftotal);
	});
	$(".denomination").click(function() {
		if($(this).val()=="Clear") {
			var arr = [ "txttotal1", "txttotal5", "txttotal10", "txttotal20", "txttotal50", "txttotal100", "txttotal500" ,  "txttotal1000"];
			jQuery.each(arr, function() {
				$("#"+this).val( "");
			});
			var _arr = [ "txt1", "txt5", "txt10", "txt20", "txt50", "txt100", "txt500" ,  "txt1000"];
			jQuery.each(_arr, function() {
				$("#"+this).val( "");
			});
			$("#new_tendered").val( 0);
			$("#new_tendered1").val( 0);
			
		}
	});

	
	
	
});
</script>

<?

}
else
{
?>

<span color="red">Shift has not yet started or cutoff time is not yet implemented.</span>
<?}